<?php

class database {
	var $_sql= '';
	var $_connection= '';
	var $_cursor = null;
	function database() 
	{
		global $obj_config;
		$this->_connection = @mysql_connect( $obj_config->host, $obj_config->user, $obj_config->password);
		if (!$this->_connection ) 
		{
			die( "Trang web dang tam dung bao duong. Se hoat dong tro lai trong thoi gian som nhat. (1.1)");
		}
		$db=$obj_config->db;
		if ($db != '' && !mysql_select_db( $db, $this->_connection )) 
		{
			die( "Trang web dang tam dung bao duong. Se hoat dong tro lai trong thoi gian som nhat. (1.2)");
		}
		
	}
	function SetQuery( $sql) 
	{
		$this->_sql = $sql;
	}
	function Query() 
	{	
		$this->_cursor = mysql_query( $this->_sql, $this->_connection );
		return $this->_cursor;
	}
	function LoadFirstRow() {
		if (!($cur = $this->Query())) {
			return null;
		}
		$ret = null;
		if ($row = mysql_fetch_row( $cur )) {
			$ret = $row[0];
		}
		mysql_free_result( $cur );
		
		return $ret;
	}
		function loadOneRow() {
		if (!($cur = $this->query())) {
			return null;
		}
		$row = mysql_fetch_assoc( $cur );
		return $row;
	}
	
	function GetNumRows() {
		return mysql_num_rows($this->_cursor);
	}
	function LoadAllRow() {
		if (!($cur = $this->Query())) {
			return null;
		}
		$array = array();
		while ($row = mysql_fetch_assoc( $cur )) 
		{
			$array[] = $row;
		}
		mysql_free_result( $cur );
		return $array;
	}
	function Disconnect() 
	{
		mysql_close( $this->_connection );
	}
	function EscapeData($data)
	{
		if(ini_get('magic_quotes_gpc'))
		{	$data = stripslashes($data);
		}
		return mysql_real_escape_string($data);
	}
}
?>